<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { giftClassifyList } from '@/pinia/getter';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import xmImage from '@/components/xmImage/index.vue';
  import Pagination from "@/components/Pagination/index.vue";
  import giftForm from './giftForm.vue';
  import { ElMessageBox } from 'element-plus';
  const giftFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const initialQuery = {
    page: 1,
    limit: 20,
    sort: "+id",
    gift_id: "",
    gift_name: "",
    min_price: "",
    max_price: "",
    order: "",
    order_prop: "",
    gift_type:""
  }
  const listQuery = reactive({
    ...initialQuery
  })
  const handleSerach = () => {
  listQuery.page = 1;
  getList();
};
const resetQuery = () => {
  Object.assign(listQuery, {
    ...initialQuery
  });
};
  const handleForm = (row:any = {})=>{
    giftFormRef.value.openDialog(row)
  }
  // 接口功能已取消
  // const handleDel = (row:any = {})=>{
  //   const { gift_id } = row
  //   if(!gift_id) return ToastWarning('gift_id error')
  //   ElMessageBox.confirm('确定删除此礼物吗？', '提示',
  //   {
  //     confirmButtonText: '确定',
  //     cancelButtonText: '取消',
  //     type: 'warning',
  //   }).then(() => {
  //     const loading = Toastloading()
  //     propApi.deleteGift(gift_id).then(()=>{
  //       ToastSuccess('删除成功')
  //       loading.close()
  //     }).catch(()=>{
  //       loading.close()
  //     })
  //   }).catch(() => { })
  // }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await propApi.getGifts(listQuery)
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">礼物ID：</div>
          <el-input v-model="listQuery.gift_id" placeholder="请输入礼物ID"/>
        </div>
        <div class="labelItem">
          <div class="label">礼物名称：</div>
          <el-input v-model="listQuery.gift_name" placeholder="请输入礼物名称"/>
        </div>
        <div class="labelItem">
          <div class="label">礼物类型：</div>
          <el-select v-model="listQuery.gift_type" placeholder="请选择礼物类型">
            <el-option
              label="全部礼物"
              :value="''"
            />
            <el-option
              v-for="item in giftClassifyList"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">礼物价格：</div>
          <el-input v-model="listQuery.min_price" placeholder="正整数最小金额"/>
          <el-input v-model="listQuery.max_price" placeholder="正整数最大金额"/>
        </div>
      </div>
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
          <el-button type="primary" @click="handleForm()">新增礼物</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="gift_sort" label="排序系数" />
      <el-table-column width="100" prop="gift_id" label="礼物ID" />
      <el-table-column min-width="120" prop="name" label="礼物名称" />
      <el-table-column width="140" prop="image" label="礼物图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.image" :list="[scoped.row.image]" />
        </template>
      </el-table-column>
      <el-table-column width="140" prop="gift_type" label="礼物类型">
        <template #default="scoped">
          {{ giftClassifyList.find(res=>res.type == scoped.row.gift_type)?.label || scoped.row.gift_type }}
        </template>
      </el-table-column>
      <el-table-column min-width="120" sortable prop="price" label="价格" />
      <el-table-column min-width="120" sortable prop="wealth_num" label="富豪值" />
      <el-table-column min-width="120" sortable prop="hot_num" label="热度值" />
      <el-table-column width="100" prop="ext_data.to_gift_id" label="抵扣礼物ID" />
      <el-table-column width="100" prop="is_show" label="是否显示">
        <template #default="scoped">
          <div>
            <el-icon>
              <SuccessFilled v-if="scoped.row.is_show == 1" style="color: #67C23A;" />
              <CircleCloseFilled v-if="scoped.row.is_show == 0" style="clear: #F56C6C;" />
            </el-icon>
          </div>
        </template>
      </el-table-column>
      <el-table-column min-width="150" show-overflow-tooltip prop="animate_resource" label="动画" />
      <el-table-column width="140" fixed="right" label="操作">
        <template #default="scoped">
          <el-button v-if="giftClassifyList.find(res=>res.type == scoped.row.gift_type)?.editSet" type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <!-- <el-button type="danger" @click="handleDel(scoped.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <giftForm ref="giftFormRef" @update-list="getList" />
  </div>
</template>